<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\FileManagerLogic;
use app\cms\validate\FileManagerValidate;
use support\AlibabaCloud;
use support\Request;
use support\Response;

/**
 * 文件管理控制器
 */
class FileManagerController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new FileManagerLogic();
        $this->validate = new FileManagerValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            'file_name'
        ]);
        $query = $this->logic->search($where);
        if (!empty($where['file_name'])) {
            $query->whereOr('rule_remark', 'like', '%' . $where['file_name'] . '%');
        }
        $data = $this->logic->getList($query);
        return $this->success($data);
    }


    /**
     * 第三方接口，识别护照信息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-29
     * @param Request $request
     * @return void
     */
    public function identifyPassport(Request $request)
    {
        $url = $request->input('url', '');
        if (empty($url)) {
            return $this->fail('请上传护照图片');
        }

        $res = AlibabaCloud::main($url);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail('识别失败');
        }
    }


    public function getCountry(Request $request): Response
    {
        $where = $request->more([
            ['name', ''],
            ['parent_id', '']
        ]);
        $data = $this->logic->tree($where);
        return $this->success($data);
    }
}
